const query = require('../utils/DB')
class adminModel {
    // public private protruce  
    static doLogin(username) {
        return query(`select * from admin where username='${username}'`)
    }

    static addToken(uid, token, exp) {
        return query(`insert into token(uid,token,exp) values(${uid},'${token}',${exp})`)
    }
	static addMemToken(uid, token, exp) {
	    return query(`insert into token(mid,token,exp) values(${uid},'${token}',${exp})`)
	}
    static async getUser(username, page = 1, pagesize = 10) {
        let nowpage = (page - 1) * pagesize
        let res = await query(`select *,(select name  from role as a ,adminrole as b  where a.rid =b.rid and b.uid=admin.id ) as rolename from admin  where username like'%${username}%' limit ${nowpage},${pagesize}`)
        let count = await query(`select count(*) as num from admin where username like'%${username}%'`)
        return { res, count }
    }

    static addUser(username, password, nickname) {
        return query(`insert into admin(username,password,nickname) values('${username}','${password}','${nickname}')`)
    }

    static edit(id, password, nickname) {
        return query(`update admin set password='${password}',nickname='${nickname}' where id=${id}`)
    }

    static del(id) {
        return query(`delete from admin where id=${id}`)
    }
	
	static checkToken(token){
		return query(
			`select * from token where token='${token}' and exp>${new Date().getTime()}`)
	}
}

module.exports = adminModel
